<template>
  <div class="update-name">
  <van-nav-bar title="设置昵称" left-text="取消" 
  right-text="完成" 
  @click-left="$emit('close')"
  @click-right="onConfirm"
  />
  <div class="field-wrap">
    <van-field
      v-model="upname"
      rows="2"
      autosize
      type="textarea"
      maxlength="7"
      placeholder="请输入昵称"
      show-word-limit
    />
  </div>
</div>
</template>

<script>
import {updateUserprofile} from '@/api/user.js'
export default {
  data(){
    return{
      upname:this.value
    }
  },
  props:{
    value: {
    type: String,
    required: true
  }
  },
  methods:{
  async onConfirm(){
    this.$toast.loading({
      message:'保存中...',
      forbidClick:true,
      duration:0,
    })
   try {
    if (!this.upname.length) {
          this.$toast('昵称不能为空')
          return
        }
    await updateUserprofile({name:this.upname})
     // 更新视图
     this.$emit('input', this.upname)
        // 关闭弹出层
        this.$emit('close')
        // 提示成功
        this.$toast.success('更新成功')

   } catch (error) {
    this.$toast.fail('更新失败')
   }
    }
  }
}
</script>

<style lang="less" scoped>
.field-wrap {
  padding: 20px;
}
</style>